package com.jasmine.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * @author : jasmineXz
 */
public class BeanUtil extends cn.hutool.core.bean.BeanUtil {
    private static final Logger log = LoggerFactory.getLogger(BeanUtil.class);

    /**
     * 对象拷贝 (数据源为空返回空)
     * @param from 数据源
     * @param to 目标对象
     * @param <T> 范型
     * @return 数据源为空返回空
     */
    public static <T> T copyObject(Object from, Class<T> to) throws Exception {
        if (null == from) return null;
        Object dest;
        dest = to.newInstance();
        copyProperties(from , dest);
        return (T) dest;
    }

    /**
     * 拷贝集合
     * @param from 数据源
     * @param to 目标
     * @param <T> 范型
     * @return 数据源为空返回空
     */
    public <T> List<T> copyList(List<T> from, Class to) throws Exception {
        List<T> dest = new ArrayList<>();
        from.forEach((Object e) -> {
            try {
                T toObject = (T) to.newInstance();
                copyProperties(e, toObject);
                dest.add(toObject);
            } catch (Exception e1) {
                log.error("拷贝集合错误:" + e1);
            }
        });
        return dest;
    }
}
